package com.ybxiang.camera_opengl.utils

import android.util.Size
import androidx.camera.core.CameraX
import androidx.camera.core.Preview
import androidx.camera.core.PreviewConfig
import androidx.lifecycle.LifecycleOwner
import com.ybxiang.camera_opengl.CameraRender

/**
 * Created by yunbo.xiang on 2021/12/15
 * Describe:
 */
object CameraHelper {
    /**
     * 使用后置摄像头
     */
    private val currentFacing = CameraX.LensFacing.BACK
    fun init(lifecycleOwner: LifecycleOwner, listener: Preview.OnPreviewOutputUpdateListener) {
        CameraX.bindToLifecycle(lifecycleOwner, getPreview(listener))
    }

    private fun getPreview(listener: Preview.OnPreviewOutputUpdateListener): Preview {
        // 分辨率并不是最终的分辨率，CameraX会自动根据设备的支持情况，结合你的参数，设置一个最为接近的分辨率
        val previewConfig = PreviewConfig.Builder()
            .setTargetResolution(Size(640,480))
            .setLensFacing(currentFacing) // 前置或后置摄像头
            .build()
        val preview = Preview(previewConfig)
        preview.onPreviewOutputUpdateListener = listener
        return preview
    }
}